package com.yx.gsyd.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.yx.gsyd.entity.SysManager;
import com.yx.gsyd.entity.search.TaskListSearch;
import com.yx.gsyd.entity.view.*;
import com.yx.gsyd.service.IMainCountService;
import com.yx.gsyd.service.ISysManagerService;
import com.yx.gsyd.service.ITaskListService;
import com.yx.gsyd.service.IWxUserService;
import com.yx.gsyd.util.DateUtils;
import com.yx.gsyd.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/admin/echart")
public class MainController {

    @Autowired
    private IWxUserService iWxUserService;

    @Autowired
    private ITaskListService iTaskListService;

    @Autowired
    private IMainCountService iMainCountService;

    @Autowired
    private ISysManagerService iSysManagerService;

    /**
     * 总数统计
     * @return
     */
    @GetMapping("/mainCount")
    public ResultView mainCount() {
        SysManager sysManager=this.iSysManagerService.getById(StpUtil.getLoginIdAsString());
        TaskListSearch obj=new TaskListSearch();
        if(sysManager.getMType()==1||sysManager.getMType()==3){
            System.out.println("全部权限");
            obj.setAdminId("all");
        }else{
            if(sysManager.getMType()==2){
                obj.setFwzxzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==4){
                obj.setJhtdzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==5){
                obj.setYhtdzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==6){
                SysManager parent=this.iSysManagerService.getById(sysManager.getParentManagerId());
                if(parent.getMType()==2){
                    obj.setFwzxygManagerId(sysManager.getManagerId());
                }
                if(parent.getMType()==4){
                    obj.setJhtdygManagerId(sysManager.getManagerId());
                }
                if(parent.getMType()==5){
                    obj.setYhtdygManagerId(sysManager.getManagerId());
                }
            }
        }
        AdminMainView adminMainView=new AdminMainView();
        adminMainView.setAllDataCountView(this.iMainCountService.getAllDataCountView(obj));
        adminMainView.setDfpDataCountView(this.iMainCountService.getDfpDataCountView(obj));
        adminMainView.setYfpDataCountView(this.iMainCountService.getYfpDataCountView(obj));
        adminMainView.setDfpYhDataCountView(this.iMainCountService.getDfpYhDataCountView(obj));
        adminMainView.setYfpYhDataCountView(this.iMainCountService.getYfpYhDataCountView(obj));
        adminMainView.setWwjDataCountView(this.iMainCountService.getWwjDataCountView(obj));
        adminMainView.setYwjDataCountView(this.iMainCountService.getYwjDataCountView(obj));
        adminMainView.setYjjDataCountView(this.iMainCountService.getYjjDataCountView(obj));
        adminMainView.setYtgDataCountView(this.iMainCountService.getYtgDataCountView(obj));
        return ResultView.ok(adminMainView);
    }


    /**
     * 业务列表折线图
     * @return
     */
    @GetMapping("/taskCount")
    public ResultView taskCount() throws ParseException {
        SysManager sysManager=this.iSysManagerService.getById(StpUtil.getLoginIdAsString());
        TaskListSearch obj=new TaskListSearch();
        if(sysManager.getMType()==1||sysManager.getMType()==3){
            System.out.println("全部权限");
            obj.setAdminId("all");
        }else{
            if(sysManager.getMType()==2){
                obj.setFwzxzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==4){
                obj.setJhtdzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==5){
                obj.setYhtdzrManagerId(sysManager.getManagerId());
            }
            if(sysManager.getMType()==6){
                SysManager parent=this.iSysManagerService.getById(sysManager.getParentManagerId());
                if(parent.getMType()==2){
                    obj.setFwzxygManagerId(sysManager.getManagerId());
                }
                if(parent.getMType()==4){
                    obj.setJhtdygManagerId(sysManager.getManagerId());
                }
                if(parent.getMType()==5){
                    obj.setYhtdygManagerId(sysManager.getManagerId());
                }
            }
        }
        Date oneyue=DateUtils.getFirstDayOfMonth(new Date());
        Date twoyue=DateUtils.getLastDayOfMonth(new Date());
        //统计当前月份所有日期
        List<String> dateList= DateUtils.getDaysBetweenTwoDays(oneyue,twoyue);
        List<AdminChartView> arrayList=new ArrayList<>();
        List<AdminChartView> adminChartViewList=this.iMainCountService.getAdminChart(obj);
        if(adminChartViewList!=null&&adminChartViewList.size()>=1){
            ir:for (String day : dateList) {
                int i=0;
                for (AdminChartView adminChartView : adminChartViewList) {
                    i++;
                    if(day.equals(adminChartView.getDayStr())){
                        arrayList.add(adminChartView);
                        continue ir;
                    }else{
                        if(adminChartViewList.size()==i){//没有的日期，默认补充0
                            adminChartView=new AdminChartView();
                            adminChartView.setDayStr(new SimpleDateFormat("dd").format(DateUtils.stringToDate(day)));
                            adminChartView.setCount(0);
                            arrayList.add(adminChartView);
                        }
                    }
                }
            }
        }else{
            for (String day : dateList) {
                AdminChartView adminChartView=new AdminChartView();
                adminChartView.setDayStr(new SimpleDateFormat("dd").format(DateUtils.stringToDate(day)));
                adminChartView.setCount(0);
                arrayList.add(adminChartView);
            }
        }
        return ResultView.ok(arrayList);
    }


    public static void main(String[] args) throws ParseException {
        Date oneyue=DateUtils.getFirstDayOfMonth(new Date());
        Date twoyue=DateUtils.getLastDayOfMonth(new Date());
        List<String> dateList= DateUtils.getDaysBetweenTwoDays(oneyue,twoyue);
        for (String s : dateList) {
            System.out.println(s);
        }
    }



}
